local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local OfflinePkDefine = require("offlinepk.Define")
local CopyDefine = require("copy.Define")
local CopyCommon = require("copy.CopyCommon")
local CopyHandler = require("copy.HandlerL")
local Broadcast  = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local OfflinePkLogic = require("offlinepk.OfflinePkLogicL")
local OfflinePkExcel = require("excel.OfflinePk")
local Grid = require("bag.Grid")
local BagLogic =require("bag.BagLogic")
local ShopHelp = require("shop.ShopHelp")
local VipLogic = require("vip.VipLogic")
local VipDefine = require("vip.VipDefine")

function CG_OFFLINE_PK_START(human, msg)
    if human.db.offlinePkRank < 1 then
        return
    end
    if os.time() - human.db.offlinePkTime < OfflinePkDefine.OFFLINE_PK_COOLDOWN then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_NO_COOLDOWN)
        return
    end
    local enterTimes = CopyCommon.getCopyEnterCount(human, CopyDefine.COPY_OFFLINE_PK_ID) 
    if enterTimes >= CopyCommon.getCopyMaxEnterCount(human, CopyDefine.COPY_OFFLINE_PK_ID) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_NO_CNT)
        return       
    end
    OfflinePkLogic.startPk(human, msg.uuid)    
end

function CG_OFFLINE_PK_ADD_CNT(human)
    local cnt = VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGR_OFFLINEPK)
    local addCnt = CopyCommon.getCopyVipAddCnt(human, CopyDefine.COPY_OFFLINE_PK_ID)
    if cnt == 0 then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.TASK_ADD_CNT_NOT_VIP)
        return 
    end
    if cnt <= addCnt then
        if VipLogic.getVipLv(human) >= 10 then
          Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_TOP)
        else
          Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.COPY_PATA_ADD_FULL)
        end
        return
    end
    if ObjHuman.getYuanbao(human) < 10 then
      ShopHelp.onNoRMB(human)
      return
    end   
    ObjHuman.decYuanbao(human, 10, "offlinepk") 
    CopyHandler.addVipCopyCnt(human, CopyDefine.COPY_OFFLINE_PK_ID)
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.ACT_COPY_ADD_CNT_OK)    
    
    OfflinePkLogic.updateSelfInfo(human)     
end

function CG_OFFLINE_PK_COOLDOWN(human)
    if os.time() - human.db.offlinePkTime >= OfflinePkDefine.OFFLINE_PK_COOLDOWN then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_NO_COOLDOWN_DEC)
        return
    end
    if VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGR_OFFLINEPKCD) ~= -1 then
        if ObjHuman.getYuanbao(human) < 50 then
            Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_NO_RMB_FOR_CD)
            ShopHelp.onNoRMB(human)
            return
        end
        ObjHuman.decYuanbao(human, 50, "offlinepk")
    end
    OfflinePkLogic.decCD(human)
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_CD_OK)
end

local RewardCache
function CG_OFFLINE_PK_REWARD_QUERY(human)
    local msg = Msg.gc.GC_OFFLINE_PK_REWARD_QUERY
    if RewardCache == nil then
        RewardCache = {}
        local len = 0
        for i = 1, #OfflinePkExcel.rewardRank do  
            if OfflinePkExcel.rewardRank[i].maxRank <= 100 then
              len = len + 1
              local rewardConf = OfflinePkExcel.rewardRank[i].reward
              local maxLen = #rewardConf <=5 and #rewardConf or 5
              for j = 1, maxLen do
                local grid = {}
                Grid.create(grid, rewardConf[j][1], rewardConf[j][2], rewardConf[j][3])
                Grid.makeItem(grid,msg.list[len].reward[j])
              end
              msg.list[len].reward[0] = maxLen
              msg.list[len].minRank =  OfflinePkExcel.rewardRank[i].minRank
              msg.list[len].maxRank =  OfflinePkExcel.rewardRank[i].maxRank
            end
        end
        msg.list[0] = len
    end
    Msg.send(msg, human.fd)
end

function CG_OFFLINE_PK_GET_REWARD(human)
    if 1 then
        return
    end
    if human.db.offlinePkRank < 1 then
        return
    end
    if human.db.offlinePkRewardRank == 0 then
        return
    end
    local conf
    for i = 1, #OfflinePkExcel.rewardRank do
        conf = OfflinePkExcel.rewardRank[i]
        if human.db.offlinePkRewardRank >= conf.minRank and (conf.maxRank == -1 or human.db.offlinePkRewardRank <= conf.maxRank) then
            break    
        end
        conf = nil
    end
    if not BagLogic.checkCanAddItemList(human, conf.reward, #conf.reward) then
        Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.SHOP_ERR_BAG_FULL)
        return
    end
    for j = 1, #conf.reward do
        BagLogic.addItem(human, conf.reward[j][1], conf.reward[j][3],conf.reward[j][2], "offlinepk")
    end
    human.db.offlinePkRewardRank = 0
    Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.OFFLINE_PK_GET_REWARD_OK)
    OfflinePkLogic.updateSelfInfo(human)
end
    